package social.hub.google.buzz;

import java.util.List;

import javax.ws.rs.core.MediaType;

import social.hub.util.RequestData;
import social.hub.util.UrlBuilder;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.client.filter.LoggingFilter;
import com.sun.jersey.api.json.JSONConfiguration;
import com.sun.jersey.oauth.client.OAuthClientFilter;
import com.sun.jersey.oauth.signature.OAuthParameters;
import com.sun.jersey.oauth.signature.OAuthSecrets;

/**
 * http://code.google.com/intl/pt-BR/apis/buzz/v1/using_rest.html#people
 * 
 * @author Andre
 */
public class BuzzClientImpl implements BuzzClient {

	private static final String API_URL_BASE = "https://www.googleapis.com/buzz/v1/";

	private static final String USER_URL_BASE = API_URL_BASE + "people/{user_id}/";
	private static final String ACTIVITY_URL_BASE = API_URL_BASE + "activities/{user_id}/";

	private static final String USER_PROFILE = USER_URL_BASE + "@self";
	private static final String USER_FOLLOWERS = USER_URL_BASE + "@groups/@followers";
	private static final String USER_FOLLOWEDS = USER_URL_BASE + "@self";

	private static final String ACTIVITY_NEW = ACTIVITY_URL_BASE + "@self";

	private static final String SELF_ID = "@me";

	private static final String BUZZ_SCOPE = "https://www.googleapis.com/auth/buzz";

	private Client client;

	public BuzzClientImpl(String consumerToken, String consumerTokenSecret, String accessToken,
			String accessTokenSecret) {

		ClientConfig clientConfig = new DefaultClientConfig();
		clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);

		client = Client.create(clientConfig);

		OAuthParameters oauthParameters = new OAuthParameters().consumerKey(consumerToken).token(
				accessToken);
		OAuthSecrets oauthSecrets = new OAuthSecrets().consumerSecret(consumerTokenSecret)
				.tokenSecret(accessTokenSecret);

		oauthParameters.put("scope", BUZZ_SCOPE);

		client.addFilter(new LoggingFilter());

		OAuthClientFilter filter = new OAuthClientFilter(client.getProviders(), oauthParameters,
				oauthSecrets);

		client.addFilter(filter);

	}

	@Override
	public List<BuzzProfile> getFolloweds() {
		return getFolloweds(SELF_ID);
	}

	@Override
	public List<BuzzProfile> getFolloweds(String buzzId) {

		String url = new UrlBuilder(USER_FOLLOWEDS).addUrlParan("user_id", buzzId)
				.addQueryParan("alt", "json").toString();

		BuzzPersonFeedResponse feedResponse = client.resource(url)
				.get(BuzzPersonFeedResponse.class);

		List<BuzzProfile> followers = feedResponse.getData().getEntry();

		return followers;

	}

	@Override
	public List<BuzzProfile> getFollowers() {
		return getFollowers(SELF_ID);
	}

	@Override
	public List<BuzzProfile> getFollowers(String buzzId) {

		String url = new UrlBuilder(USER_FOLLOWERS).addUrlParan("user_id", buzzId)
				.addQueryParan("alt", "json").toString();

		BuzzPersonFeedResponse feedResponse = client.resource(url)
				.get(BuzzPersonFeedResponse.class);

		List<BuzzProfile> followers = feedResponse.getData().getEntry();

		return followers;

	}

	@Override
	public BuzzProfile getUserProfile() {
		return getUserProfile(SELF_ID);
	}

	@Override
	public BuzzProfile getUserProfile(String buzzId) {

		String url = new UrlBuilder(USER_PROFILE).addUrlParan("user_id", buzzId)
				.addQueryParan("alt", "json").toString();

		BuzzPersonResponse response = client.resource(url).get(BuzzPersonResponse.class);

		BuzzProfile profile = response.getData();

		return profile;

	}

	@Override
	public void createAtivity(String status) {
		createAtivity(status, SELF_ID);
	}

	@Override
	public void createAtivity(String status, String buzzId) {

		String url = new UrlBuilder(ACTIVITY_NEW).addUrlParan("user_id", buzzId).toString();

		BuzzActivity ativity = new BuzzActivity();
		ativity.setContent(status);

		BuzzRequestAtivity requestAtivity = new BuzzRequestAtivity();
		requestAtivity.setObject(ativity);

		RequestData<BuzzRequestAtivity> request = new RequestData<BuzzRequestAtivity>(
				requestAtivity);

		client.resource(url).type(MediaType.APPLICATION_JSON_TYPE)
				.accept(MediaType.APPLICATION_JSON).post(request);

	}

}
